<template>
	<view class="container-nav">
		<view class="w-nav">
			<view class="nav-title" @click="onClkTitle(0)">
				<text :class="{'nav-focus':active==0}">发起提交</text>
			</view>
			<view class="nav-title" @click="onClkTitle(1)">
				<text :class="{'nav-focus':active==1}">查看数据</text>
			</view>
		</view>
		<view class="nav-body" v-show="active==0">
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>请假类型</view>
					</view>
					<view class="row-later-time">
						<picker @change="onPickerChange" :value="lt_arr[lt_index].id" :range="lt_arr" :range-key="'name'">
							{{lt_arr[lt_index].name}}
						</picker>
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>开始时间</view>
					</view>
					<view class="row-later-time" @click="onSelDatePicker(0)">
						{{BeginTime}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>结束时间</view>
					</view>
					<view class="row-later-time" @click="onSelDatePicker(1)">
						{{EndTime}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>请假理由</view>
					</view>
					<view class="row-later-time">
						<editor placeholder="请输入" id="t_editor" @input="onLaterTime" @ready="onTextReady"></editor>
					</view>
				</view>
			</view>
			<view class="w-btns">
				<button type="primary" :disabled="blnDisabled_w" @click="onAddLeavesInfo">{{blnEditStatus?"更新":"提交"}}</button>
			</view>
		</view>
		<view class="nav-body" v-show="active==1">
			<view class="w-box" v-for="(item,index) in items" :key="index" @click.stop="onNavTo(item)">
				<view class="w-box-title">
					<view class="w-box-title-img">{{_retSlgName(item.Leaver)}}</view>
					<view>
						<view class="w-box-title-header">
							{{item.Leaver}}提交的请假
						</view>
						<view class="w-box-title-dt">
							请假类型：{{item.LeaveType}}
						</view>
						<view class="w-box-title-dt">
							开始时间：{{_retFormatDate(item.BeginTime)}}
						</view>
						<view class="w-box-title-dt">
							结束时间：{{_retFormatDate(item.EndTime)}}
						</view>
						<view class="w-box-title-act">
							<view class="act-status">
								{{item.FlowStatus_Name}}
							</view>
							<view class="act-edit" v-show="_retBtnsVal(item.ValidBtn,'vj-btn-edit')" 
								  @click.stop="onEditLeaves(item.ID)">
								修改
							</view>
							<view class="act-cancel" v-show="_retBtnsVal(item.ValidBtn,'vj-btn-del')" 
								  @click.stop="onDeleLeaves(item.ID)">
								撤销
							</view>
							<view class="pass" v-show="_retStatusVal(item.FlowStatus,0)">
								已完成
							</view>
							<view class="refuse" v-show="_retStatusVal(item.FlowStatus,1)">
								已拒绝
							</view>
						</view>
					</view>
					<view class="w-box-title-org">
						{{_retFormatYmdt(item.Createtime)}}
					</view>
				</view>
			</view>
			<view class="w-box" v-show="items.length==0">
				{{loading}}
			</view>
		</view>
		<date-time-picker ref='date-time' :endYear="endYear" type='datetime' @change='onDateTimeChange'></date-time-picker>
	</view>
</template>

<script>
	let pageApp = getApp();
	import DateTimePicker from '@/components/uni-dateTimePicker/uni-dateTimePicker.vue'
	export default {
		data() {
			return {
				active: 0,
				endYear: 2030,
				loading: "数据正在加载中...",
				lt_arr: JSON.parse(uni.getStorageSync("leavetype")),
				lt_index: 0,
				BeginTime: "请选择",
				_BeginTime: "",
				EndTime: "请选择",
				_EndTime: "",
				StartOrEnd: 0,
				FlowStatus: 0,
				items: [],
				Reason: "",
				id: -1,
				blnDisabled_w: true,
				blnEditStatus: false
			}
		},
		components: {
			DateTimePicker
		},
		watch: {
			BeginTime: {
				handler: function(n, o) {
					if (n) {
						this._BeginTime = n.length == 16 ? n + ":00" : n;
						this.diffDateTime(this._EndTime, this._BeginTime);
					}
				}
			},
			EndTime: {
				handler: function(n, o) {
					if (n) {
						this._EndTime = n.length == 16 ? n + ":00" : n;
						this.diffDateTime(this._EndTime, this._BeginTime);
					}
				}
			}
		},
		onLoad: function(options) {
			// console.log(options.id);
			if (options.id) {
				this.blnEditStatus = true;
				this.id = options.id;
				this.editLeavesInfo(this.id);
			}
		},
		onShow: function() {
			if (this.active) {
				// 加载请假历史
				this.getLeavesInfo();
			}
		},
		methods: {
			onNavTo(item) {
				if (this._retBtnsVal(item.ValidBtn, 'vj-btn-edit') && this._retBtnsVal(item.ValidBtn, 'vj-btn-del')) {
					return false;
				} else {
					if (item.ID) {
						uni.navigateTo({
							url: "./LeavesInfo?id=" + item.ID
						})
					} else {
						uni.showToast({
							title: "正在开发中..."
						})
					}
				}
			},
			onTextReady(e) {
				if (this.blnEditStatus) {
					uni.createSelectorQuery().select('#t_editor').context((res) => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.Reason
						})
					}).exec()
				}
			},
			_retFormatDate(v) {
				return v.substr(0, v.length - 3)
			},
			_retSlgName(v){
				return (v)?v.substr(0,1):"";
			},
			_retFormatYmdt(v) {
				return this.retFormatYmdt(v);
			},
			_retBtnsVal(v, t) {
				return v.indexOf(t) != -1 ? true : false;
			},
			_retStatusVal(v, t) {
				let _tmp_arr_refuse = [595,596];
				let _tmp_arr_status = JSON.parse(uni.getStorageSync("leaves_status"));
				switch (t) {
					case 0: //完成
						return v == _tmp_arr_status[1].id ? true : false;
						break;
					case 1: //拒绝
						return (v == _tmp_arr_refuse[0] || v == _tmp_arr_refuse[1]) ? true : false;
						break;
				}
			},
			diffDateTime(e, b) {
				// console.log(e,b);
				if (e && b) {
					// 两个时间相减
					let _e = new Date(Date.parse(e.replace(/-/g, "/")));
					let _b = new Date(Date.parse(b.replace(/-/g, "/")));
					// 相差值
					let _t = _e - _b;
					if (_t <= 0) {
						uni.showToast({
							icon: "none",
							title: "结束时间必须大于开始时间！",
							success: () => {
								this.blnDisabled_w = true;
							}
						})
					} else {
						this.blnDisabled_w = this.checkIsEmpty(this.Reason, "00000");
					}
				}
			},
			onLaterTime(e) {
				this.Reason = e.detail.text;
				this.blnDisabled_w = this.checkIsEmpty(this.Reason, "00000");
				this.diffDateTime(this._EndTime, this._BeginTime);
			},
			onClkTitle(e) {
				this.active = e;
				if (e) {
					// 加载请假历史
					this.getLeavesInfo();
				}
			},
			onPickerChange(e) {
				this.lt_index = e.target.value;
			},
			onSelDatePicker(index) {
				this.StartOrEnd = index;
				this.$refs['date-time'].show();
			},
			onDateTimeChange(value) {
				if (this.StartOrEnd) {
					this.EndTime = value;
				} else {
					this.BeginTime = value;
				}
			},
			onAddLeavesInfo() {
				// console.log(this.lt_index, this.lt_arr[this.lt_index], this.Reason, this._BeginTime, this._EndTime)
				let _url = pageApp.globalData.url + "/LeavesNew";
				let _data = {
					"Leaver": JSON.parse(uni.getStorageSync("userLoginData")).UserID,
					"LeaveType": this.lt_index + 1,
					"FlowStatus": this.FlowStatus,
					"Reason": this.Reason,
					"BeginTime": this._BeginTime,
					"EndTime": this._EndTime,
					"TerminateTime": "",
					"OrgID": JSON.parse(uni.getStorageSync("userLoginData")).OrgID,
					"DepartmentID": JSON.parse(uni.getStorageSync("userLoginData")).OrgID
				}
				if (this.blnEditStatus) {
					_url = pageApp.globalData.url + "/LeavesUpdate";
					_data.ID = this.id;
				}
				//准备数据
				let apiData = {
					url: _url,
					data: _data,
					method: "POST",
					success: (d) => {
						if (d.data.code == "001") {
							uni.showToast({
								icon: "success",
								title: "操作成功！"
							})
							setTimeout(() => {
								if (this.blnEditStatus) {
									uni.navigateBack({
										delta: 1
									})
								} else {
									this.onClkTitle(1);
								}

							}, 1000)
						} else {
							uni.showToast({
								icon: "fail",
								title: "操作失败！"
							})
						}
					}
				}
				//发送数据请求
				this.vjAjaxData(apiData);
			},
			getLeavesInfo() {
				// 准备数据
				let apiData = {
					url: pageApp.globalData.url + "/LeavesList",
					data: {
						"page": 1,
						"rows": 10
					},
					success: (d) => {
						// console.log("数据", d)
						if (d.data.code == "001") {
							this.items = d.data.data.items;
							if (this.items.length == 0) {
								this.loading = "目前还没有找到请假申请！"
							}
						}
					}
				}
				// 发送数据
				this.vjAjaxData(apiData);
			},
			editLeavesInfo(ID) {
				// 准备数据
				let apiData = {
					url: pageApp.globalData.url + "/LeavesInfo",
					data: {
						"ID": ID
					},
					success: (d) => {
						// console.log("数据", d.data.data)
						if (d.data.code == "001") {
							this.BeginTime = d.data.data.BeginTime;
							this._BeginTime = this.BeginTime;
							this.EndTime = d.data.data.EndTime;
							this._EndTime = this.EndTime;
							this.lt_index = d.data.data.LeaveType-1;
							this.FlowStatus = d.data.data.FlowStatus
							this.Reason = this.Base64.unescape(d.data.data.Reason);
						}
					}
				}
				// 发送数据
				this.vjAjaxData(apiData);
			},
			onEditLeaves(id) {
				uni.navigateTo({
					url: "./LeavesNew?id=" + id
				})
			},
			onDeleLeaves(id) {
				uni.showModal({
					title: "微简系统",
					content: "真的要撤销申请吗？",
					success: (res) => {
						if (res.confirm) {
							// 准备请求数据
							let apiData = {
								url: pageApp.globalData.url + "/LeavesDelete",
								data: {
									"ID": id
								},
								method: "POST",
								success: (e) => {
									uni.showToast({
										title: "撤销成功！"
									})
									this.getLeavesInfo();
								}
							}
							// 发送数据请求
							this.vjAjaxData(apiData);
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F1F5F6;
	}

	.container-nav {
		font-size: 14px;
		line-height: 24px;
	}

	.ql-container {
		height: 100rpx;
		min-height: 100rpx;
	}

	.w-nav {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
		background-color: #FFFFFF;
		padding: 20rpx 0;
	}

	.nav-focus {
		border-bottom: solid 6rpx #4497e1;
		padding-bottom: 20rpx;
	}

	.nav-title {
		width: 50%;
		text-align: center;
	}

	.nav-tip {
		position: relative;
		padding: 10rpx;
		display: flex;
		width: 100%;
		top: -30rpx;
		background-color: #4497e1;
		color: #ffffff;
		font-size: 26rpx;
		justify-content: space-around;
	}

	.w-box {
		margin: 20rpx 0;
		padding: 40rpx;
		background-color: #ffffff;
		position: relative;

	}

	.w-box .pass {
		border: solid 6rpx #a7e3cf;
		position: absolute;
		right: 0;
		bottom: 0;
		width: 140rpx;
		height: 140rpx;
		text-align: center;
		line-height: 140rpx;
		border-radius: 50%;
		color: #a7e3cf;
		font-weight: 700;
		transform: rotateZ(-30deg);
	}

	.w-box .refuse {
		border: solid 6rpx #f2bbb7;
		position: absolute;
		right: 0;
		bottom: 0;
		width: 140rpx;
		height: 140rpx;
		text-align: center;
		line-height: 140rpx;
		border-radius: 50%;
		color: #f2bbb7;
		font-weight: 700;
		transform: rotateZ(-30deg);
	}

	.row-later-title {
		display: flex;
		font-size: 34rpx;
	}

	.later-title-dot {
		padding: 8rpx;
		color: red;
	}

	.row-later-time {
		margin-left: 30rpx;
	}

	.w-box-title {
		display: flex;
		position: relative;
	}

	.w-box-title-img {
		width: 60rpx;
		height: 60rpx;
		padding: 4rpx;
		text-align: center;
		line-height: 60rpx;
		border-radius: 16rpx;
		background-color: #4497e1;
		margin-right: 16rpx;
		margin-top: 10rpx;
		color: #ffffff;
		font-size: 34rpx;
	}

	.w-box-title-header {
		font-size: 34rpx;
		color: #000000;
		font-weight: 700;
	}

	.w-box-title-dt {
		font-size: 28rpx;
		line-height: 34rpx;
	}

	.w-box-title-act {
		display: flex;
		justify-content: space-between;
	}

	.act-status {
		color: #E1B176
	}

	.act-cancel,
	.act-edit {
		position: absolute;
		color: #4497e1;
	}

	.act-cancel {
		right: 0;
	}

	.act-edit {
		right: 100rpx;
	}

	.w-box-title-org {
		position: absolute;
		margin-top: 16rpx;
		right: 0;
	}

	.w-box-row {
		font-size: 30rpx;
		line-height: 36rpx;
	}

	.w-box-row text {
		overflow: hidden;
		display: inline-block;
		text-overflow: ellipsis;
		white-space: nowrap;
		width: 670rpx;
	}

	.w-btns {
		background-color: #ffffff;
		padding: 20rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}

	.w-btns button {
		width: 710rpx;
		margin-left: 0;
	}
</style>
